package com.sl.mes.app.service.puboid;

import com.sl.mes.app.controller.ApiRequestTools;
import com.sl.mes.db.dao.PubOidMapper;
import com.sl.mes.db.itf.oid.IdGenerator;
import com.sl.mes.db.itf.puboid.IPubOid;
import com.sl.mes.db.vo.PubOid;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class IdGeneratorService implements IdGenerator {
    public static long OID_BASE_INITIAL_VAL = 19000000000000L;

    String dataSource = "dsign";

    @Resource
    private IPubOid iPubOid;

    private static final int OID_AMOUNT = 1000;


    public String generate() {
        String groupNumber = "0001";

        String ds = this.dataSource;

        return OidGenerator.getInstance().nextOid(ds, groupNumber);
    }

    public String[] generate(int amount) {
        String[] newOids = new String[amount];
        String groupNumber = "0001";
        String ds = this.dataSource;
        for (int i = 0; i < amount; i++) {
            newOids[i] = OidGenerator.getInstance().nextOid(ds, groupNumber);
        }
        return newOids;
    }


    public String generate(String pk_corp) {
        if (pk_corp == null) {
            throw new IllegalArgumentException("Can't generate primary key with null pk_corp");
        }

        return OidGenerator.getInstance().nextOid(this.dataSource, pk_corp);
    }


    public String[] generate(String pk_corp, int amount) {
        if (pk_corp == null) {
            throw new IllegalArgumentException("Can't generate primary key with null pk_corp");
        }

        String[] newOids = new String[amount];
        if (1 == 2) {
            for (int i = 0; i < amount; i++) {
                OID_BASE_INITIAL_VAL += 1L;
                newOids[i] = OidBaseAlgorithm.getInstance(String.valueOf(OID_BASE_INITIAL_VAL)).nextOidBase();
            }

            return newOids;
        }

        if (this.dataSource == null) {
            for (int i = 0; i < amount; i++) {
                newOids[i] = OidGenerator.getInstance().nextOid(pk_corp);
            }
        } else {
            for (int i = 0; i < amount; i++) {
                newOids[i] = OidGenerator.getInstance().nextOid(this.dataSource, pk_corp);
            }
        }
        return newOids;
    }





    /**
     * 新增
     *
     * @param pk_corp
     * @param oid
     * @throws Exception
     */
    private void insertOidBase(String pk_corp, String oid) {
        //"insert into PUB_OID (group, pub_oid) values(?,?)"
        PubOid pubOid =new PubOid();
        pubOid.setPubOid(oid);
        pubOid.setGroup(pk_corp);
        iPubOid.insertPubOid(pubOid);


    }

    private String selectForUpdateOidBase(String pk_corp) {

        String currentId = null;
        String firstId = "10000000000000";

        //TODO:先从数据库里查询id
        PubOid pubOid =iPubOid.findPubOidById(pk_corp);


        if (pubOid!=null) {//
            currentId = pubOid.getPubOid();
            if (currentId.compareTo("10000000000000") < 0) {
                currentId = firstId;
            }

            //用数据库里查询出来的id，查找一个id
            currentId = nextOidCode(currentId);

            //再更新Id
            //("update PUB_OID set pub_oid=? where group=?");
            pubOid.setPubOid(currentId);
            pubOid.setGroup(pk_corp);
            iPubOid.updataPubOid(pubOid);



        } else {
            //新增
            insertOidBase(pk_corp, nextOidCode(firstId));
            currentId = firstId;
        }

        return currentId;
    }

    private String nextOidCode(String currentId) {
        OidBaseAlgorithm oidBaseAlgrithm = OidBaseAlgorithm.getInstance(currentId);
        String nextOidBaseCode = null;

        for (int j = 0; j < 1000; j++) {
            nextOidBaseCode = oidBaseAlgrithm.nextOidBase();
        }
        return nextOidBaseCode;
    }



}

 